﻿#region Using
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.Text;
using System.IO;
#endregion // Using

namespace ConsoleHarness
{
    class Program
    {
        [DataContract]
        public class GoogleSearchResults
        {
            [DataMember(Name="responseData")]
            public ResponseData ResponseData { get; set; }
        }

        [DataContract]
        public class ResponseData
        {
            [DataMember(Name="results")]
            public IEnumerable<Result> Results { get; set; }
        }

        [DataContract]
        public class Result
        {
            [DataMember(Name = "unescapedUrl")]
            public string Url { get; set; }

            [DataMember(Name = "titleNoFormatting")]
            public string Title { get; set; }

            [DataMember(Name = "content")]
            public string Content { get; set; }
        }
        
        static void DoTest()
        {
            string json = "{\"responseData\": {\"results\":[{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.mountvernonplantation.com/mtvernon/eform_detail.asp?id\u003d2\u0026name\u003dLogin+Request\",\"url\":\"http://www.mountvernonplantation.com/mtvernon/eform_detail.asp%3Fid%3D2%26name%3DLogin%2BRequest\",\"visibleUrl\":\"www.mountvernonplantation.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:QsH0A-pjzycJ:www.mountvernonplantation.com\",\"title\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation - eForm\",\"titleNoFormatting\":\"Mount Vernon Plantation - eForm\",\"content\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation.\"},{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.mountvernonplantation.com/mtvernon/nocookies.asp\",\"url\":\"http://www.mountvernonplantation.com/mtvernon/nocookies.asp\",\"visibleUrl\":\"www.mountvernonplantation.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:jcQNN_V5YOgJ:www.mountvernonplantation.com\",\"title\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation - No Cookies\",\"titleNoFormatting\":\"Mount Vernon Plantation - No Cookies\",\"content\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation.\"},{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.mountvernonplantation.com/mtvernon/item_list.asp?subcat\u003d23\u0026subtitle\u003dHospitals\",\"url\":\"http://www.mountvernonplantation.com/mtvernon/item_list.asp%3Fsubcat%3D23%26subtitle%3DHospitals\",\"visibleUrl\":\"www.mountvernonplantation.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:6J2-bkVRQhwJ:www.mountvernonplantation.com\",\"title\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation - - Hospitals\",\"titleNoFormatting\":\"Mount Vernon Plantation - - Hospitals\",\"content\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation.\"},{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.mountvernonplantation.com/mtvernon/classified.asp?filt\u003d\u0026ad_id\u003d24\",\"url\":\"http://www.mountvernonplantation.com/mtvernon/classified.asp%3Ffilt%3D%26ad_id%3D24\",\"visibleUrl\":\"www.mountvernonplantation.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:ozd4YazxlokJ:www.mountvernonplantation.com\",\"title\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation - Classifieds -\",\"titleNoFormatting\":\"Mount Vernon Plantation - Classifieds -\",\"content\":\"\u003cb\u003eMount\u003c/b\u003e Vernon Plantation.\"}],\"cursor\":{\"resultCount\":\"23\",\"pages\":[{\"start\":\"0\",\"label\":1},{\"start\":\"4\",\"label\":2},{\"start\":\"8\",\"label\":3},{\"start\":\"12\",\"label\":4},{\"start\":\"16\",\"label\":5},{\"start\":\"20\",\"label\":6}],\"estimatedResultCount\":\"23\",\"currentPageIndex\":0,\"moreResultsUrl\":\"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dsite:mountvernonplantation.com+Mount\",\"searchResultTime\":\"0.09\"}}, \"responseDetails\": null, \"responseStatus\": 200}";
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(GoogleSearchResults));
            using(MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            { 
                GoogleSearchResults data = serializer.ReadObject(stream) as GoogleSearchResults;
                foreach (Result result in data.ResponseData.Results)
                {
                    Console.WriteLine(result.Title + " - " + result.Url);
                    Console.WriteLine(result.Content);
                }
            } 
        }// of DoTest()

        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

            DoTest();

            watch.Stop();
            Console.WriteLine("Elapsed: {0}", watch.Elapsed);
            Console.ReadKey();
        }// of Main(string[])
    }// of class Program
}// of namespace ConsoleHarness